#include "dominion.h"
//#include "dominion_helpers.h"
//#include "testdominion.h"
//#include <string.h>
#include <stdio.h>
//#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include "rngs.h"

const int NUM_CARD_TYPES = 26;
const int KINGDOM_CARDS = 10;

void initializeCardArray(int *deck);
void shuffleCardArray(int *deck);
int buyRandActionCard(int *game_deck);
void printGameSummary(struct gameState G,FILE *out, int gameNumber, int numPlayers);
int countMoney(struct gameState G);
int getActionCard(struct gameState G);
//int k[];


int main (int argc, char** argv) 
{
  printf ("Testing full game of dominion.\n");
  FILE *out;
  out=fopen("gameResults.out", "w");
  struct gameState G;
  int seed = 4;

  int game_deck[KINGDOM_CARDS],i,gameNumber;
  //static int k[NUM_CARD_TYPES];
	int k[NUM_CARD_TYPES];
  SelectStream(2);
  PutSeed(seed);
  srand(time(NULL));

  for(gameNumber=0;gameNumber <100;gameNumber++)
  {
    initializeCardArray(k);
    shuffleCardArray(k);
    fprintf(out, "Game Number %d\n", gameNumber );
    //Pick first 10 random cards
    for(i=0;i<KINGDOM_CARDS;++i)
    {
      game_deck[i] = k[i];
      fprintf(out,"Kingdom card[%d]: %d\n",i,game_deck[i]);
    }

    //2-4 players required
    int numPlayers = rand()%2+2;

    fprintf(out, "Number of Players: %i \n", numPlayers);
    fprintf(out,"Starting game.\n");

    initializeGame(numPlayers, game_deck, seed, &G);

    int money = 0;
    int actionCard = -1;
    int randomActionCard;

    while (!isGameOver(&G)) 
    {
      money = 0;
      money = countMoney(G);
      actionCard = getActionCard(G);

      if (actionCard != -1) 
      {
        fprintf(out,"%d: action card # %d played\n",G.whoseTurn, handCard(actionCard, &G));
        fprintf(out,"Handpos: %d\n",actionCard);
        playCard(actionCard, 0, 0, 0, &G);
      }

      //Buy phase
      randomActionCard = buyRandActionCard(game_deck);

      if (money >= 8) 
      {
        fprintf(out,"%d: bought province\n",G.whoseTurn);
        buyCard(province, &G);
      }
      else if (money >= 6) 
      {
        fprintf(out,"%d: bought # %d card\n",G.whoseTurn,randomActionCard);
        buyCard(randomActionCard, &G);
      }
      else if (money >= 3)
      {
        fprintf(out,"%d: bought silver\n",G.whoseTurn);
        buyCard(silver, &G);
      }
      else if(money == 0)
      {
        fprintf(out,"%d: bought copper\n",G.whoseTurn);
        buyCard(copper, &G);
      }
      fprintf(out,"%d: endTurn\n",G.whoseTurn);
      endTurn(&G);
    }
    printGameSummary(G, out, gameNumber, numPlayers);
  }
    printf ("ALL TESTS OK.\n");
    return 0;
}

void initializeCardArray(int *deck)
{
        int i = 0;
        for(i=0;i<NUM_CARD_TYPES;++i)
                deck[i]=i;
}

void shuffleCardArray(int *deck)
{
        int i = 0;
        int j = 0;
        int tmp;
        for(i=0;i<NUM_CARD_TYPES;++i)
  {
                j = rand()%NUM_CARD_TYPES;
                tmp  = deck[j];
                deck[j] = deck[i];
                deck[i] = tmp;
        }
}

int countMoney(struct gameState G)
{
  int i, money = 0;
  for (i = 0; i < numHandCards(&G); i++) 
  {
    if (handCard(i, &G) == copper)
      money++;
    else if (handCard(i, &G) == silver)
      money += 2;
    else if (handCard(i, &G) == gold)
      money += 3;
  }
  return money;
}

int getActionCard(struct gameState G)
{
  int i, actionCard = -1;
  for (i = 0; i<numHandCards(&G);i++)
  {
    if(handCard(i,&G)!= gold && handCard(i,&G)!= silver && handCard(i,&G)!= copper)
      return actionCard = handCard(i,&G);
  }
  return -1;
}

int buyRandActionCard(int *game_deck)
{
        int i;
        i = rand()%11;
        return game_deck[i];
}

void printGameSummary(struct gameState G,FILE *out, int gameNumber, int numPlayers)
{
        int i, j;
        fprintf(out,"\n\n");
        fprintf(out,"Game %d over:\n",gameNumber);
        
    // print scores
    for(i=0;i<27;i++){
                fprintf(out,"Supply Count for [%d]: %d\n",i,G.supplyCount[i]);
    }
        
  for(i=0;i<numPlayers;i++)
  {
        fprintf(out,"Stats for Player %d\n", i);
    fprintf(out,"Score: %d\n", scoreFor(i, &G));
    fprintf(out,"Deck: ");
                for(j=0;j<G.deckCount[i];j++){
                        fprintf(out,"%d, ",G.deck[i][j]);
        }
        fprintf(out,"\n");
            fprintf(out,"Player %d's hand: ",i);
                for(j=0;j<G.handCount[i];j++){
                        fprintf(out,"%d, ",G.hand[i][j]);
                }
        fprintf(out,"\n");

        fprintf(out,"Player %d's discard: ",i);
                for(j=0;j<G.discardCount[i];j++){
                        fprintf(out,"%d, ",G.discard[i][j]);
                }
        fprintf(out,"\n");

  fprintf(out,"Player %d's card summary: ",i);
    for(j=0;j<G.discardCount[i];j++){
      fprintf(out,"%d, ",G.discard[i][j]);
    }
  fprintf(out,"\n\n");
        
        }
}
